<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  components: {},
  data() {
    return {
      load: true,
      form: {
        // AppID
        appId: "",
        // AppSecret
        appSecret: "",
        // 消息加密密钥
        encodingAesKey: "",
        // 是否开启 0否 1是
        isOpen: 0,
        // 微信号
        mpAccount: "",
        // 公众号名称
        mpName: "",
        // 原始ID
        originalId: "",
        // 令牌
        serverToken: "",
        // 服务器地址
        serverUrl: "",
      },
      // 页面接口地址
      apiUrl: {
        detail: "mall-site-portal/wechat-mp-config/getItem",
        save: "mall-site-portal/wechat-mp-config/saveOrUpdate",
      },
    };
  },
  methods: {
    // 初始化
    init() {
      this.getData();
    },

    // 获取内容信息
    async getData() {
      this.load = true;
      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
        },
        window
      );
      if (_data._ok) {
        for (let i in this.form) {
          let _d = _data.data[i];
          if (_d != null) {
            this.form[i] = _d;
          }
        }
      }

      // 将脱敏的appSecret 赋值到表单中
      this.form.appSecret = _data.data.appSecretDesensitized;

      this.load = false;
    },

    // 提交
    async submit() {
      if (!(await this.validate())) {
        return;
      }
      this.load = true;
      let _data = await this.$vFN.Http.post(
        {
          url: this.apiUrl.save,
          data: this.form,
        },
        window
      );
      if (_data._ok) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.init();
      }
      this.load = false;
    },

    // 验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false);
        });
      });
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>